package com.example.wc.logindemo.MyServiceDemo;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;

import com.example.wc.logindemo.Blog;
import com.example.wc.logindemo.BlogLab;
import com.example.wc.logindemo.EditBlogActivity;

import java.text.ParseException;
import java.util.List;

import static com.example.wc.logindemo.EditBlogActivity.SERVICE_BLOG_DATE;
import static com.example.wc.logindemo.EditBlogActivity.SERVICE_BLOG_ID;
import static com.example.wc.logindemo.EditBlogActivity.SERVICE_EDITBLOGACTIVITY_CONTEXT;

/**
 * Created by hui on 2017/3/26.
 */

public class MyService extends Service {

    public static final String TAG = "MyService";
//    public static final String SERVICE_BLOG_DATE = "com.example.wc.logindemo.blog_date";

    private MyBinder mBinder = new MyBinder();

    private String mId;
    private BlogLab mBlogLab;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate() executed");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
//        mId = intent.getStringExtra(SERVICE_BLOG_ID);

        new Thread(new Runnable() {
            @Override
            public void run() {
                BlogLab blogLab = BlogLab.get(getApplicationContext());
                List<String> listBlogId = blogLab.getBlogsId();

                if(listBlogId != null){
                    for(int i = 0; i<listBlogId.size(); i++){
                        mId = listBlogId.get(i);
                        try {
                            blogLab.deleteBlogByDate(mId);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }).start();

        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        int anHour = 60 * 60 * 1000; // 15*1000;//一小时的毫秒数
        long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
        Intent i = new Intent(this, AlarmReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
        //从 Android 4.4 版本开始，Alarm 任务的触发时间将会变得不准确.使用 AlarmManager的 setExact()方法来替代 set()方法，就可以保证任务准时执行了

        Log.d(TAG, "onStartCommand() executed");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy() executed");
    }

    @Override
    public IBinder onBind(Intent intent) {
//        mId = intent.getStringExtra(SERVICE_BLOG_ID);
//        dateString = intent.getStringExtra(SERVICE_BLOG_DATE);
//        Log.d("id", mId);
//        Log.d("dateString", dateString);
        return mBinder;
    }

    public class MyBinder extends Binder {

        public void startSaveBlog() {

//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        Thread.sleep(1*1000);
//                        BlogLab blogLab = BlogLab.get(getApplicationContext());
//                        blogLab.deleteBlogById(mId);//根据dateString查询Blog
//                        Log.d("TAG", "gogogo!");
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }

//                }
//            }).start();

//            Log.d("TAG", "gogogo!");
            // 执行具体的下载任务
        }

    }

}
